﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Sinacor.Infra.UI.Controls.SmartClient;
using Sinacor.Infra.UI.Security.Licensing;
using Sinacor.Infra.UI.Session;
using Sinacor.Infra.UI.Authentication;
using Sinacor.Infra.Service.Data;
using Sinacor.UI.Application.Menu.SmartClient;
using Sinacor.UI.Application.Settings.SmartClient;


namespace Sinacor.Infra.InputTester.View.SmartClient
{
    /// <summary>
    /// Interaction logic for InputTesterView.xaml
    /// </summary>
    public partial class InputTesterView : SinacorWindow
    {

        #region Propriedades

        /// <summary>
        /// Armazena as configurações das propriedades
        /// </summary>
        public InputTester.Model.Propriedade PropertyContainer { get; set; }

        #endregion

        #region Construtor

        /// <summary>
        /// Construtor padrão
        /// </summary>
        public InputTesterView()
        {
            //
            InitializeComponent();
            //
            this.DataContext = this;
            PropertyContainer = new InputTester.Model.Propriedade();
            this.AfterInitialize += new RoutedEventHandler(InputTesterView_AfterInitialize);
        }

        #endregion

        #region Eventos

        /// <summary>
        /// AfterInitialize
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void InputTesterView_AfterInitialize(object sender, RoutedEventArgs e)
        {
            this.prpMinValue.IsEnabled = false;
            this.prpMaxValue.IsEnabled = false;

            this.ConstruirIdentificaInvestidor();
        }

        /// <summary>
        /// prpIsRequired Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void prpIsRequired_Click(object sender, RoutedEventArgs e)
        {
            this.RemoverIdentificaInvestidor();
            this.ConstruirIdentificaInvestidor();
        }

        /// <summary>
        /// prpIntegers LostFocus
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void prpIntegers_LostFocus(object sender, RoutedEventArgs e)
        {
            if (this.prpIntegers.Value != null)
            {
                if (this.prpIntegers.Value != 0)
                {
                    this.AtualizarPropriedadeIntegers();

                    this.prpMinValue.IsEnabled = true;
                    this.prpMaxValue.IsEnabled = true;
                }
                else
                {
                    this.prpMinValue.IsEnabled = false;
                    this.prpMaxValue.IsEnabled = false;
                }
            }
            else
            {
                this.prpMinValue.IsEnabled = false;
                this.prpMaxValue.IsEnabled = false;
            }

        }

        /// <summary>
        /// prpMaxValue LostFocus
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void prpMaxValue_LostFocus(object sender, RoutedEventArgs e)
        {
            this.AtualizarPropriedadeMaxValue();
        }

        /// <summary>
        /// prpMinValue LostFocus
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void prpMinValue_LostFocus(object sender, RoutedEventArgs e)
        {
            this.AtualizarPropriedadeMinValue();
        }

        /// <summary>
        /// Limpa tela
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLimpa_Click(object sender, RoutedEventArgs e)
        {
            this.LimparTela();
        }

        /// <summary>
        /// ChkShowCPF Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkShow_Click(object sender, RoutedEventArgs e)
        {
            this.RemoverIdentificaInvestidor();
            this.ConstruirIdentificaInvestidor();
        }

        #endregion

        #region Métodos

        /// <summary>
        /// Constrói o controle SinacorIdenticaInvestidor, conforme propriedades configuradas
        /// </summary>
        private void ConstruirIdentificaInvestidor()
        {
            //
            SinacorIdentificadorInvestidor controle = new SinacorIdentificadorInvestidor();
            //
            controle.Name = "IdentificadorInvestidor";
            controle.HorizontalAlignment = HorizontalAlignment.Left;
            controle.VerticalAlignment = VerticalAlignment.Top;
            controle.Margin = new Thickness(150, 40, 0, 0);
            controle.IsRequired = this.PropertyContainer.IsRequired;
            //controle.SetBinding(SinacorIdentificadorInvestidor.IsRequiredProperty, "PropertyContainer.IsRequired");
            controle.ShowCPF = this.PropertyContainer.ShowCPF;
            controle.ShowCNPJ = this.PropertyContainer.ShowCNPJ;
            controle.ShowClienteEstrangeiro = this.PropertyContainer.ShowClienteEstrangeiro;
            controle.ShowCVM = this.PropertyContainer.ShowCVM;
            controle.ShowOutros = this.PropertyContainer.ShowOutros;
            //
            this.GrdIdentifica.Children.Add(controle);
        }

        /// <summary>
        /// Remove controle identifica investidor
        /// </summary>
        private void RemoverIdentificaInvestidor()
        {
            this.GrdIdentifica.Children.RemoveAt(5);
        }

        /// <summary>
        /// Atualiza propriedades dos controles
        /// </summary>
        private void AtualizarPropriedadeIntegers()
        {
            //
            this.Int16TextBox.UpdateTarget(SinacorInt16TextBox.IntegersProperty);
            this.Int32TextBox.UpdateTarget(SinacorInt32TextBox.IntegersProperty);
            this.Int64TextBox.UpdateTarget(SinacorInt64TextBox.IntegersProperty);
            this.DecimalTextBox.UpdateTarget(SinacorDecimalTextBox.IntegersProperty);
            this.DoubleTextBox.UpdateTarget(SinacorDoubleTextBox.IntegersProperty);
            //
            this.ZerarValorControles();
        }

        /// <summary>
        /// Atualiza propriedade MaxValue
        /// </summary>
        private void AtualizarPropriedadeMaxValue()
        {
            //
            this.Int16TextBox.UpdateTarget(SinacorInt16TextBox.MaxValueProperty);
            this.Int32TextBox.UpdateTarget(SinacorInt32TextBox.MaxValueProperty);
            this.Int64TextBox.UpdateTarget(SinacorInt64TextBox.MaxValueProperty);
            this.DecimalTextBox.UpdateTarget(SinacorDecimalTextBox.MaxValueProperty);
            this.DoubleTextBox.UpdateTarget(SinacorDoubleTextBox.MaxValueProperty);
            //
            this.ZerarValorControles();
        }

        /// <summary>
        /// Atualiza propriedade MinValue
        /// </summary>
        private void AtualizarPropriedadeMinValue()
        {
            //
            this.Int16TextBox.UpdateTarget(SinacorInt16TextBox.MinValueProperty);
            this.Int32TextBox.UpdateTarget(SinacorInt32TextBox.MinValueProperty);
            this.Int64TextBox.UpdateTarget(SinacorInt64TextBox.MinValueProperty);
            this.DecimalTextBox.UpdateTarget(SinacorDecimalTextBox.MinValueProperty);
            this.DoubleTextBox.UpdateTarget(SinacorDoubleTextBox.MinValueProperty);
            //
            this.ZerarValorControles();
        }

        /// <summary>
        /// Zera valor dos controles
        /// </summary>
        private void ZerarValorControles()
        {
            this.Int16TextBox.Value = null;
            this.Int32TextBox.Value = null;
            this.Int64TextBox.Value = null;
            this.DecimalTextBox.Value = null;
            this.DoubleTextBox.Value = null;
        }

        /// <summary>
        /// Limpa tela
        /// </summary>
        private void LimparTela()
        {
            //
            this.ZerarValorControles();
            //
            this.CepTextBox.Clear();
            this.CnpjTextBox.Clear();
            this.CpfTextBox.Clear();
            this.CvmTextBox.Clear();
            this.EmailTextBox.Clear();
            this.InvestidorEstrangeiroTextBox.Clear();
            this.NyseTextBox.Clear();
            //
            this.TextBox.Clear();
            this.DatePicker.Value = null;
            this.TimePicker.Value = null;
            //
            this.LimparPropriedades();
            //
            this.RemoverIdentificaInvestidor();
            this.ConstruirIdentificaInvestidor();
        }

        /// <summary>
        /// Limpa propriedades
        /// </summary>
        private void LimparPropriedades()
        {
            //
            this.prpIsFormated.IsChecked = false;
            this.prpAcceptsNegative.IsChecked = false;
            this.prpAutoFill.IsChecked = false;
            this.prpIsRequired.IsChecked = false;
            //
            this.prpMinValue.Value = null;
            this.prpMaxValue.Value = null;
            this.prpDecimals.Value = null;
            this.prpIntegers.Value = null;
            //
            this.prpShowCPF.IsChecked = false;
            this.prpShowCNPJ.IsChecked = false;
            this.prpShowCVM.IsChecked = false;
            this.prpShowOutros.IsChecked = false;
            this.prpShowClienteEstrangeiro.IsChecked = false;
        }

        #endregion

    }
}
